﻿Imports VSModel
Imports Cls_Common
Partial Class Control_WhatCookToDay_WhatCookTodayByCat
    Inherits System.Web.UI.UserControl
    Dim iFirstSubMenu As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("MenuId") Is Nothing Then
                Dim iMenuId As Integer = Page.RouteData.Values("MenuId")
                hidMenuId.Value = iMenuId.ToString
                LoadNewsRelated(1, hidMenuId.Value)
            End If
        End If
    End Sub
    'Show cac bai viet lien quan
    Protected Sub LoadNewsRelated(ByVal iPage As Integer, ByVal iobjId As Integer)
        Using data As New VSEntities
            Dim q = (From ct In data.Contents Where ct.StatusId = 3 And ct.MenuId = iobjId And ct.TypeId = 1 And ct.PublishedDate <= Now _
                  And (ct.eEffectiveDate >= Now Or ct.eEffectiveDate Is Nothing) Order By ct.Created Descending Select ct).ToList
            Dim h = (From g In data.Menus Where g.MenuId = iobjId Select g).FirstOrDefault
            If Not h Is Nothing Then
                hplTitle.Text = IIf(IsNothing(h.Title) = True, "", h.Title)
            End If

            Excute_Javascript("SetPageTitle('" & sMetaTitle & " | Trang " & iPage.ToString & "');", Me.Page, True)
            Dim r = q.Skip((iPage - 1) * 10).Take(10).ToList
            hidCount.Value = q.Count
            If (q.Count > 0) Then
                Create_Pager(hidCount.Value, iPage, 10, 10)
            End If
            With rptArticalRelate
                .DataSource = r
                .DataBind()
            End With
        End Using
    End Sub
#Region "Bind Data bai viet lien quan"
    Protected Sub rptArticalRelate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptArticalRelate.ItemDataBound
        Dim p = CType(e.Item.DataItem, Object)
        Dim imgDes As Image = CType(e.Item.FindControl("imgDes"), Image)
        If Not p.Image Is Nothing Then
            imgDes.ImageUrl = p.Image.ToString
        End If

        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        hplImg.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = p.Title
        hplTitle.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"

        Dim ltrDes As Label = CType(e.Item.FindControl("ltrDes"), Label)
        If Not p.Description Is Nothing Then
            ltrDes.Text = p.Description.ToString
        End If

    End Sub
#End Region

#Region "Cac thu tuc phan trang bai viet lien quan"
    Protected Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        If TotalPage > 1 And Page_Index <> 1 Then
            lnkFirst.Enabled = True
        Else
            lnkFirst.Enabled = False
        End If

        If TotalPage > 1 And Page_Index <> TotalPage Then
            lnkLast.Enabled = True
        Else
            lnkLast.Enabled = False
        End If
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
        If rptPage.Items.Count > 0 Then
            If Page_Index = 1 And rptPage.Items.Count > 1 Then
                lnkFirst.Enabled = False
                lnkLast.Enabled = True
            ElseIf Page_Index = rptPage.Items.Count Then
                lnkFirst.Enabled = True
                lnkLast.Enabled = False
            Else
                lnkFirst.Enabled = True
                lnkLast.Enabled = True
            End If
        End If
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        lnkTitle.CausesValidation = False
        'dang ky async ko postback
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.CssClass = "active"
            lnkTitle.Text = e.Item.DataItem
        Else
            '  lnkTitle.CssClass = "pagesLink"
            lnkTitle.Text = e.Item.DataItem
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        LoadNewsRelated(CInt(lnkTile.ToolTip.ToString), hidMenuId.Value)

       
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click

        hidCur_Page.Value = hidCur_Page.Value - 1

        LoadNewsRelated(CInt(hidCur_Page.Value), hidMenuId.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        LoadNewsRelated(CInt(hidCur_Page.Value), hidMenuId.Value)
    End Sub
#End Region
End Class
